<?php echo $header; ?>

<?php if(isset($error_warning) && $error_warning != '') { ?> 
    <div class="warning"><?php echo $error_warning; ?></div>
<?php } ?>
<?php if(isset($success) && $success != ''){ ?>
    <div class="success"><?php echo $success; ?></div>
<?php } ?>

<div id="dialog" title="Confirm" style="display: none;">
    <p>Delete cannot be undone! Are you sure you want to do this?</p>
</div>
<div id="dialog1" title="Confirm" style="display: none;">
    <p>Please select an item to delete.</p>
</div>

<div class="box">
    <div class="left"></div>
    <div class="right"></div>
    <div class="heading">
        <h1 style="background-image: url('view/image/mail.png');"><?php echo $text_subscriber_title; ?></h1>
        <div class="buttons">
            <a id="insert" class="button" href="<?php echo $insert; ?>"><span><?php echo $text_insert; ?></span></a>
            <a id="delete" class="button" href="#"><span><?php echo $text_delete; ?></span></a>
			<a id="update_cc" class="button" href="#"><span>Export subscriber lists</span></a>
        </div>
    </div>
    
    <div class="content">
        <form id="edit_subscriber" action="<?php echo $action; ?>" method="post" enctype="multipart/form-data">
            <table class="list">
                <thead>
                    <tr>
                        <td width="1" style="text-align: center;"><input type="checkbox" name="selected[]" onclick="jQuery('input[name*=\'selected\']').attr('checked', this.checked);" /></td>
                        <td class="left" width="30%">
                            <?php if ($sort == 'email') { ?>
                                <a href="<?php echo $sort_email; ?>" class="<?php echo strtolower($order); ?>"><?php echo $text_email; ?></a>
                            <?php } else { ?>
                                <a href="<?php echo $sort_email; ?>"><?php echo $text_email; ?></a>
                            <?php } ?>
                        </td>
                        <td class="left" width="40%">
                            <?php if ($sort == 'name') { ?>
                                <a href="<?php echo $sort_name; ?>" class="<?php echo strtolower($order); ?>"><?php echo $text_name; ?></a>
                            <?php } else { ?>
                                <a href="<?php echo $sort_name; ?>"><?php echo $text_name; ?></a>
                            <?php } ?>
                        </td> 
                        <td class="left">
                            <?php if ($sort == 'date_added') { ?>
                                <a href="<?php echo $sort_date; ?>" class="<?php echo strtolower($order); ?>"><?php echo $text_date; ?></a>
                            <?php } else { ?>
                                <a href="<?php echo $sort_date; ?>"><?php echo $text_date; ?></a>
                            <?php } ?>
                        </td>                            
                        <td class="right">Action</td>
                    </tr>
                </thead>
                <tbody>                                                       
                    <tr class="filter">                                 
                        <td></td>
                        <td><input type="text" name="filter_email" value="<?php echo $filter_email; ?>" size="50" /></td>
                        <td><input type="text" name="filter_name" value="<?php echo $filter_name; ?>" size="60" /></td>                                
                        <td><input type="text" id="filter_date" name="filter_date" value="<?php echo $filter_date; ?>" size="12" /></td>
                        <td align="right"><a onclick="filter();" class="button"><span><?php echo $button_filter; ?></span></a></td>
                    </tr>                    
                    
                    <?php if(!empty($list)){ ?>                                    
                        <?php foreach($list as $item){ ?> 
                            <tr>
                                <td class="select_item" style="text-align: center;">
                                    <input type="checkbox" value="1" name="selected[]" />
                                    <input type="hidden" name="deleted[]" value="<?php echo $item['email']; ?>" />
                                </td>
                                <td class="left"><?php echo $item['email']; ?></td>
                                <td class="left"><?php echo $item['name']; ?></td>
                                <td class="left"><?php echo $item['date']; ?></td>
                                <td class="right"><a class="edit" href="<?php echo $item['action']; ?>" rel="<?php echo $item['email']; ?>"><span><?php echo $text_edit; ?></span></a></td>                            
                            </tr>                                     
                        <?php } ?>                       
                    <?php }else{ ?>
                        <tr>
                            <td class="center" colspan="5"><?php echo $text_empty; ?></td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
            
            <input type="hidden" id="method" name="method" value="" /> 
            <input type="hidden" id="email" name="email" value="" /> 
        </form>  
        <?php if(!empty($list)){ ?>
            <div class="pagination"><?php echo $pagination; ?></div>
        <?php } ?>
    </div>
</div>

<?php echo $footer; ?>

<link type="text/css" rel="stylesheet" href="view/javascript/jquery/ui/themes/smoothness/ui.all.css" />
<script type="text/javascript" src="view/javascript/jquery/ui/ui.dialog.js"></script>
<script type="text/javascript" src="view/javascript/jquery/ui/ui.datepicker.js"></script>
<script type="text/javascript"><!--    
    jQuery(document).ready(function(){ 
        jQuery('#dialog').dialog({
            autoOpen: false,
            width: 300,
            modal: true,
            resizable: false,
            buttons: {
                "Cancel": function() { 
                    jQuery(this).dialog("close"); 
                },
                "Ok": function() { 
                    jQuery(this).dialog("close"); 
                    jQuery('#edit_subscriber').attr('action', '<?php echo $delete; ?>');
                    jQuery('#edit_subscriber').submit();
                }
            }
        }); 
        jQuery('#dialog1').dialog({
            autoOpen: false,
            width: 300,
            modal: true,
            resizable: false,
            buttons: {
                "Close": function() { 
                    jQuery(this).dialog("close"); 
                }
            }
        });       
            
        /*                          
        jQuery('#insert').click(function(even){
            even.preventDefault(); 

            jQuery('#method').val('insert');
            jQuery('#edit_subscriber').attr('action', '<?php echo $insert; ?>');
            jQuery('#edit_subscriber').submit();
        });
        */
        
        jQuery('#delete').click(function(even){
            even.preventDefault();    
            
            var checked = 0;
            jQuery('#edit_subscriber input[name="selected[]"]').each(function(){
                if(jQuery(this).is(':checked')){
                    jQuery(this).val(jQuery(this).siblings('input[name="deleted[]"]').val());
                    checked++;
                }
            });
            
            jQuery('#method').val('delete');
            
            // Remove jquery message dialog
            jQuery('#edit_subscriber').attr('action', '<?php echo $delete; ?>');
            jQuery('#edit_subscriber').submit();
            return;
            
            if(checked == 0){
                jQuery('#dialog1').dialog('open');    
            }
            else{
                jQuery('#dialog').dialog('open');    
            }
        }) 
        
        // Remove jquery message dialog
        jQuery('#edit_subscriber').submit(function(){
            if(jQuery(this).attr('action').indexOf('delete',1) != -1) {
                if (!confirm ('Delete/Uninstall cannot be undone! Are you sure you want to do this?')) {
                    return false;
                }
            }
        });
        
        $('#filter_date').datepicker({dateFormat: 'yy-mm-dd'});
		
		jQuery('#update_cc').click(function(even){
            even.preventDefault();

            window.location.href = '<?php echo $update_cc; ?>';
            /*
			jQuery.get(
				"<?php echo $update_cc; ?>", 
				{push_data: 1},
				function(data){                    
					alert(data); 
				}
			);
			*/
		})
    });
  //-->
</script>

<script type="text/javascript"><!--  
    function filter() {
        url = '<?php echo $base_url; ?>';
        
        var filter_email = $('input[name=\'filter_email\']').attr('value');      
        if (filter_email) {
            url += '&filter_email=' + encodeURIComponent(filter_email);
        }
        
        var filter_name = $('input[name=\'filter_name\']').attr('value');      
        if (filter_name) {
            url += '&filter_name=' + encodeURIComponent(filter_name);
        }
        
        var filter_date = $('input[name=\'filter_date\']').attr('value');
        if (filter_date) {
            url += '&filter_date=' + encodeURIComponent(filter_date);
        }
        
        window.location = url + '&token=<?php echo $token; ?>';
    }

    $('#edit_subscriber input').keydown(function(e) {
        if (e.keyCode == 13) { 
            filter();
            
            e.preventDefault();
        }
    });
//-->
</script>